package com.lll.simple.rpc.registry;

import com.lll.simple.rpc.common.RegistryType;

/**
 * @Author: liull
 * @Description: 用来全局生成RegistryService的工厂
 * @Date: Created in 14:44 2021/2/3
 * @Modified By:
 */
public class RegistryFactory {

    private static volatile RegistryService registryService;

    public static RegistryService getInstance(String registryAddr, RegistryType registryType) throws Exception {
        if (registryService == null) {
            synchronized (RegistryFactory.class) {
                if (registryService == null) {
                    switch (registryType) {
                        case ZOOKEEPER:
                            registryService = new ZookeeperRegistryService(registryAddr);
                            break;
                        case EUREKA:
                            registryService = new EurakaRegistryService(registryAddr);
                            break;
                        case NACOS:
                            registryService = new NacosRegistryService(registryAddr);
                            break;
                        case UNKNOWN:
                        default:
                            throw new UnsupportedOperationException();
                    }
                }
            }
        }
        return registryService;
    }
}
